package com.example.demo.nuoqi.service.impl;

import com.example.demo.nuoqi.dao.ApplyapprovalMapper;
import com.example.demo.nuoqi.pojo.Applyapproval;
import com.example.demo.nuoqi.service.ApplyapprovalService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:Applyapproval业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class ApplyapprovalServiceImpl implements ApplyapprovalService {

    @Autowired
    private ApplyapprovalMapper applyapprovalMapper;


    /**
     * Applyapproval条件+分页查询
     * @param applyapproval 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Applyapproval> findPage(Applyapproval applyapproval, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(applyapproval);
        //执行搜索
        return new PageInfo<Applyapproval>(applyapprovalMapper.selectByExample(example));
    }

    /**
     * Applyapproval分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Applyapproval> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Applyapproval>(applyapprovalMapper.selectAll());
    }

    /**
     * Applyapproval条件查询
     * @param applyapproval
     * @return
     */
    @Override
    public List<Applyapproval> findList(Applyapproval applyapproval){
        //构建查询条件
        Example example = createExample(applyapproval);
        //根据构建的条件查询数据
        return applyapprovalMapper.selectByExample(example);
    }


    /**
     * Applyapproval构建查询对象
     * @param applyapproval
     * @return
     */
    public Example createExample(Applyapproval applyapproval){
        Example example=new Example(Applyapproval.class);
        Example.Criteria criteria = example.createCriteria();
        if(applyapproval!=null){
            // 
            if(!StringUtils.isEmpty(applyapproval.getId())){
                    criteria.andEqualTo("id",applyapproval.getId());
            }
            // 
            if(!StringUtils.isEmpty(applyapproval.getItemName())){
                    criteria.andEqualTo("itemName",applyapproval.getItemName());
            }
            // 
            if(!StringUtils.isEmpty(applyapproval.getItemId())){
                    criteria.andEqualTo("itemId",applyapproval.getItemId());
            }
            // 
            if(!StringUtils.isEmpty(applyapproval.getApplicationName())){
                    criteria.andEqualTo("applicationName",applyapproval.getApplicationName());
            }
            // 
            if(!StringUtils.isEmpty(applyapproval.getApprovalPersonId())){
                    criteria.andEqualTo("applicationId",applyapproval.getApprovalPersonId());
            }
            // 
            if(!StringUtils.isEmpty(applyapproval.getCreateTime())){
                    criteria.andEqualTo("createTime",applyapproval.getCreateTime());
            }
            // 
            if(!StringUtils.isEmpty(applyapproval.getStatus())){
                    criteria.andEqualTo("status",applyapproval.getStatus());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        applyapprovalMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Applyapproval
     * @param applyapproval
     */
    @Override
    public void update(Applyapproval applyapproval){
        applyapprovalMapper.updateByPrimaryKey(applyapproval);
    }

    /**
     * 增加Applyapproval
     * @param applyapproval
     */
    @Override
    public void add(Applyapproval applyapproval){
        applyapprovalMapper.insert(applyapproval);
    }

    /**
     * 根据ID查询Applyapproval
     * @param id
     * @return
     */
    @Override
    public Applyapproval findById(Integer id){
        return  applyapprovalMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Applyapproval全部数据
     * @return
     */
    @Override
    public List<Applyapproval> findAll() {
        return applyapprovalMapper.selectAll();
    }

    @Override
    public Applyapproval get(Applyapproval applyapproval) {
        Example example = createExample(applyapproval);
        return applyapprovalMapper.selectOneByExample(example);
    }
}
